<?php
/**
 * Created by PhpStorm.
 * User: 吕冬
 * Date: 2017/7/21
 * Time: 15:51
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;

class Tourism extends Controller{
//    景点农家乐
 public  function tourism(){
     $list = Db::name('scenic_spot')->order('id desc')->paginate(5);
//     循环图片路径
     $cc = array();
     for($i=0;$i<count($list);$i++){
            $cc[$i]['img'] = explode("/",$list[$i]["img"]);
     }
     $page = $list->render();
     return view("tourism",array("data"=>$list,"page"=>$page,'cc'=>$cc));

}
//    景点添加页面
    public function tourism_app(){
        return view("Tourism/tourism_app");
}

//    景点添加
    public function app(){
        $a=$_POST['address'];
        $data= $_POST ;
        $files = request()->file('img');
        $cc ="";
//图片上传
        foreach($files as $file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
//                用/分割
                $cc .= $info->getSaveName()."/" ;
//                去最后的符号
                $newstr = substr($cc,0,strlen($cc)-1);
            } else {
                return $this->success('添加失败');
            }
        }
        //        数组转化字符串
            $data['img']= $newstr;
            $a= implode('-',$a);
            $data['address'] = $a;
            $aa = Db::table('scenic_spot')->insert($data);
            if ($aa) {
                return $this->success('添加成功','tourism');
            } else {
                return $this->success('添加失败');
            }

    }


//    景点删除
    public function tourism_delete(){
        $aa=$_GET['id'];
            $request =Db::table('scenic_spot')->where('id',$aa)->delete();
        if ($request) {
            return $this->success('删除成功','tourism');
        } else {
            return $this->success('删除失败');
        }
}

//    景点查询
    public function tourism_query(){
        $name = $_POST["key"];
        if($name==""){//判断name值是否为空
            $list = Db::name('scenic_spot')->order('id desc')->paginate(5);
//     循环图片路径
            $cc = array();
            for($i=0;$i<count($list);$i++){
                $cc[$i]['img'] = explode("/",$list[$i]["img"]);
            }
            $page = $list->render();
            return view("tourism",array("data"=>$list,"page"=>$page,'cc'=>$cc));
        }else{//不否为空进行模糊查询
            $list = Db::table("scenic_spot")->where("
                name like '%{$name}'
                or name like '%{$name}'
                or name like '%{$name}%'
                or address like '%{$name}'
                or address like '%{$name}'
                or address like '%{$name}%'
                or  Detailed_address like '%{$name}'
                or  Detailed_address like '%{$name}'
                or  Detailed_address like '%{$name}%'
                ")->order('id desc')->paginate();
                //查询 细分类的表 找到对应的类名
        }
//     循环图片路径
        $cc = array();
        for($i=0;$i<count($list);$i++){
            $cc[$i]['img'] = explode("/",$list[$i]["img"]);
        }
        $page = $list->render();
        return view("tourism",array("data"=>$list,"page"=>$page,'cc'=>$cc));
    }
//    景点修改
public function  tourism_update(){
    $get= $_GET['id'];
    $list = Db::name('scenic_spot')->where('id',$get)->paginate();
    $this->assign('list', $list);
    return $this->fetch('tourism_up_app');

}
    public function  tourism_update_app(){
        $a=$_POST['address'];
        $get= $_GET['id'];
        $data =$_POST;
            $a= implode('-',$a);
            $data['address'] = $a;
            if(Db::table('scenic_spot')->where('id',$get)->update($data)){
                $this->success('修改成功','tourism');
            }else{
                $this->error('修改失败');
            }
        }


//    路线页显示
  public function route(){
      $list = Db::name('route')->order('id desc')->paginate(5);
      $cc = array();
      for($i=0;$i<count($list);$i++){
          $cc[$i]['img'] = explode("/",$list[$i]["img"]);
      }
      $page = $list->render();
      return view("route",array("data"=>$list,"page"=>$page,"cc"=>$cc));
  }

//添加路线里的导游选择
 public function tourism_route_app(){
     $list = Db::name('guide')->paginate();
     $this->assign('list', $list);
     return $this->fetch('tourism_route_app');

 }
    //添加路线
 public function tourism_route(){
     $data= $_POST ;
     $name=$_POST['guide_name'];
     $id=Db::name('guide')->where('guide_name',$name)->find();
     $id=$id['id'];
     $data['guide_id'] = $id;
     $files = request()->file('img');
     $cc ="";
     foreach($files as $file) {
         $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
         if ($info) {
             $cc .= $info->getSaveName() . "/";
             $newstr = substr($cc, 0, strlen($cc) - 1);
         } else {
             return $this->success('添加失败');
         }
     }
//        数组转化字符串
     $data['img']= $newstr;
     $aa = Db::table('route')->insert($data);
     if ($aa) {
         return $this->success('添加成功','route');
     } else {
         return $this->success('添加失败');
     }

 }

    //    路线删除
    public function route_delete(){
        $aa=$_GET['id'];
        $request =Db::table('route')->where('id',$aa)->delete();
        if ($request) {
            return $this->success('删除成功','route');
        } else {
            return $this->success('删除失败');
        }
    }
    //    路线修改

    public function route_update()
    {
        $get = $_GET['id'];
        $list = Db::name('route')->where('id', $get)->paginate();
        $this->assign('list', $list);
        return $this->fetch('tourism_route_up');
    }
        public function route_update_app(){
            $get= $_GET['id'];
            $data =$_POST;
            $id=$_POST['guide_id'];
            $name=Db::name('guide')->where('id',$id)->find();
            $name=$name['guide_name'];
            $data['guide_name'] = $name;
                if(Db::table('route')->where('id',$get)->update($data)){
                    $this->success('修改成功','route');
                }else{
                    $this->error('修改失败');
                }

        }
    //    路线查询
    public function route_query(){
        $name = $_POST["key"];
        if($name==""){//判断name值是否为空
            $list = Db::name('route')->order('id desc')->paginate(5);
//     循环图片路径
            $cc = array();
            for($i=0;$i<count($list);$i++){
                $cc[$i]['img'] = explode("/",$list[$i]["img"]);
            }
            $page = $list->render();
            return view("route",array("data"=>$list,"page"=>$page,'cc'=>$cc));
        }else{//不否为空进行模糊查询
            $list = Db::table("route")->where("
                name like '%{$name}'
                or name like '%{$name}'
                or name like '%{$name}%'
                or introduce like '%{$name}'
                or introduce like '%{$name}'
                or introduce like '%{$name}%'
                ")->order('id desc')->paginate();
            //查询 细分类的表 找到对应的类名
        }
//     循环图片路径
        $cc = array();
        for($i=0;$i<count($list);$i++){
            $cc[$i]['img'] = explode("/",$list[$i]["img"]);
        }
        $page = $list->render();
        return view("route",array("data"=>$list,"page"=>$page,'cc'=>$cc));
    }














//    订单管理
    public  function tourism_order(){
//     return view("Tourism/tourism");
        $list = Db::name('tourism')->order('id desc')->paginate(5);
        $page = $list->render();
        return view("order",array("data"=>$list,"page"=>$page));

    }
//订单查询
    public function order_query()
    {
        $name = $_POST["key"];
        if ($name == "") {//判断name值是否为空
            $list = Db::name('tourism')->order('id desc')->paginate(5);
            $page = $list->render();
            return view("order", array("data" => $list, "page" => $page));
        } else {//不否为空进行模糊查询
            $list = Db::table("tourism")->where("
                route_name like '%{$name}'
                or route_name like '%{$name}'
                or route_name like '%{$name}%'

                ")->order('id desc')->paginate();  //查询 细分类的表 找到对应的类名
        }
        $page = $list->render();
        return view("order", array("data" => $list, "page" => $page));
    }
}

